package com.example.brazil_project.util;





import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;





public  class  jsonUtil {

	 String name;  
	public  void main(String[] args) {

//	/*解析2维json例子*/
//	    String jsonString="[{\"username\": \"your name\", \"user_json\": {\"username\": \"my name\", \"nickname\": \"your nickname\"}}]";
//	    System.out.println(jsonString);
//	    //转换成list
//	    List <Map>list = getJsonbyString(jsonString);
//	    jsonString =  getJsonbyObject(list);
//	    list = getJsonbyString(jsonString);
//	    System.out.println(jsonString);
//	    //遍历list
//	    for (Map map : list) {
//	    	Iterator iter = map.entrySet().iterator();
//	    	while(iter.hasNext()){
//	    		 Map.Entry element = (Map.Entry)iter.next();
//	    		 System.out.println(element.getKey()+":"+element.getValue());
//	    		 //第二维的list
//	    		 if(element.getKey().equals("user_json")){
//	    			 List <Map>list1 = (List) element.getValue();
//	    			 for (Map map1 : list1) {
//	    				 System.out.println(map1.get("username"));
//	    				 System.out.println(map1.get("nickname"));
//	    			 }
//	    			 
//	    			 
//
//	    		 }
//	    	}
//	    	
//		}
	}
	/***
	 * 参数bean
	 * 根据对象或者List转换String类似的json
	 * *****/
//	public static String getJsonbymap(Map map){ 
//		
//		String json = "";
//		Iterator iter = map.entrySet().iterator();
//		while(iter.hasNext()){
//   		Map.Entry element = (Map.Entry)iter.next();
//   		Log.e(element.getKey()+"", element.getValue()+"");
//   		if(json.equals("")){
//   			json = "[{"+json+"\""+element.getKey()+"\":\""+element.getValue()+"\"}";
//   		}
//   		else{
//   			
//   			json = json+",{\""+element.getKey()+"\":\""+element.getValue()+"\"}";
//   		}
////   	
////   		[
////   		{ "firstName":"Bill" , "lastName":"Gates" },
////   		{ "firstName":"George" , "lastName":"Bush" },
////   		{ "firstName":"Thomas" , "lastName": "Carter" }
////   		];
//   		
//
//   	}
//	    return json+"]";  
//		
//	}
	/***
	 * 参数bean
	 * 根据对象或者List转换String类似的json
	 * *****/
	public static String getJsonbymap(Map map){
		Iterator iter = map.entrySet().iterator();
		String json =null;
		while(iter.hasNext()){
			
			Map.Entry element = (Map.Entry)iter.next();
			String key = element.getKey().toString();
			String value =  map.get(key).toString();
			if(json==null){
				json = "[{ \""+key+"\":\""+value+"\"";
			}else{
				json = json + ",\""+key+"\":\""+value+"\"";
			}
		}
		return json +"}]";
		
	}
	
	
	/**
	 * 根据list获取json
	 * @param list
	 * @return
	 */
	public static String getJsonbylist(List<Map> list){
		String json = null;
		if(list==null|| list.size()<=0){
			return json;
		}
		for (Map map : list) {
			String mapjson = getJsonbymap(map);
			mapjson = mapjson.replace("[", "");
			mapjson = mapjson.replace("]", "");
			if(json==null){
				json = "["+mapjson;
			}
			else{
				json = json +","+mapjson;
			}
		}
		
		return json+"]";
		
	}
	/**
	 * jsonStr to Map
	 * @param jsonStr
	 * @return
	 */
	  public static Map<String, Object> getMapForJson(String jsonStr){  
	        JSONObject jsonObject ;  
	        try {  
	        	
	            jsonObject = new JSONObject(jsonStr);  
	              
	            Iterator<String> keyIter= jsonObject.keys();  
	            String key;  
	            Object value ;  
	            Map<String, Object> valueMap = new HashMap<String, Object>();  
	            while (keyIter.hasNext()) {  
	                key = keyIter.next();  
	                value = jsonObject.get(key);  
	                valueMap.put(key, value);  
	            }  
	            return valueMap;  
	        } catch (Exception e) {  
	            // TODO: handle exception  
	            e.printStackTrace();  
	          
	        }  
	        return null;  
	    }  
	
	  
	  /** 
	     * Json 转成 List<Map<>> 
	     * @param jsonStr 
	     * @return 
	     */  
	    public static List<Map<String, Object>> getlistForJson(String jsonStr){  
	        List<Map<String, Object>> list = null;  
	        try {  
	            JSONArray jsonArray = new JSONArray(jsonStr);  
	            JSONObject jsonObj ;  
	            list = new ArrayList<Map<String,Object>>();  
	            for(int i = 0 ; i < jsonArray.length() ; i ++){  
	                jsonObj = (JSONObject)jsonArray.get(i);  
	                list.add(getMapForJson(jsonObj.toString()));  
	            }  
	        } catch (Exception e) {  
	            // TODO: handle exception  
	            e.printStackTrace();  
	        }  
	        return list;  
	    }  

}
